<?php
App::uses('AppController', 'Controller');

class ChoicesController extends AppController {

	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
		$this->Choice->recursive = 0;
		$this->set('choices', $this->paginate());
	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function view($id = null) {
		if (!$this->Choice->exists($id)) {
			throw new NotFoundException(__('Invalid choice'));
		}
		$options = array('conditions' => array('Choice.' . $this->Choice->primaryKey => $id));
		$this->set('choice', $this->Choice->find('first', $options));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Choice->create();
			if ($this->Choice->save($this->request->data)) {
				$this->Session->setFlash(__('The choice has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The choice could not be saved. Please, try again.'));
			}
		}
		$questions = $this->Choice->Question->find('list');
		$this->set(compact('questions'));
	}

	function add_choice(){
		if ($this->request->is('post')) {
			$this->Choice->create();
			if ($this->Choice->save($this->request->data)) {
				$this->Session->setFlash(__('The choice has been saved'));
				$this->redirect('/questions/view/'.$this->data['Choice']['question_id']);
			} else {
				$this->Session->setFlash(__('The choice could not be saved. Please, try again.'));
			}
		}
		$questions = $this->Choice->Question->find('list');
		$this->set(compact('questions'));
	}

	/**
	 * edit method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function edit($id = null) {
		if (!$this->Choice->exists($id)) {
			throw new NotFoundException(__('Invalid choice'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Choice->save($this->request->data)) {
				$this->Session->setFlash(__('The choice has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The choice could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Choice.' . $this->Choice->primaryKey => $id));
			$this->request->data = $this->Choice->find('first', $options);
		}
		$questions = $this->Choice->Question->find('list');
		$this->set(compact('questions'));
	}

	/**
	 * delete method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	function delete($id, $id2) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for choice', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Choice->delete($id)) {
			//$this->Session->setFlash(__('Respuesta eliminada', true));
			$this->redirect('/questions/view/'.$id2);
		}
		$this->Session->setFlash(__('La respuesta no se pudo eliminar', true));
		$this->redirect(array('action' => 'view'));
	}

	
	
}
